
//util.js
import "@/excel/Blob";
/**
 * 时间戳
 * @param {*} timestamp  时间戳
 */
const timestampToTime = (timestamp) => {
    let date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-'
    let M =
        (date.getMonth() + 1 < 10 ?
            '0' + (date.getMonth() + 1) :
            date.getMonth() + 1) + '-'
    let D =
        (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
    let h =
        (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    let m =
        (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
        ':'
    let s =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return Y + M + D + h + m + s
};
/**
 * 存储sessionStorage
 */
const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    window.sessionStorage.setItem(name, content);
}

/**
 * 获取sessionStorage
 */
const getStore = name => {
    if (!name) return;
    return  window.sessionStorage.getItem(name);
}

/**
 * 删除sessionStorage
 */
const removeStore = name => {

    if (!name) return;
    if(name == '*'){
        sessionStorage.clear();
    }else{
        window.sessionStorage.removeItem(name);
    }

}

/**
 * 设置cookie
 **/
function setCookie(name, value, day) {
    let date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + '=' + value + ';expires=' + date;
};

/**
 * 获取cookie
 **/
function getCookie(name) {
    let reg = RegExp(name + '=([^;]+)');
    let arr = document.cookie.match(reg);
    if (arr) {
        return arr[1];
    } else {
        return '';
    }
};

/**
 * 删除cookie
 **/
function delCookie(name) {
    setCookie(name, null, -1);
};

// 获取排序时间戳
function noTime(){
    let yy = new Date().getFullYear();
    let mm = new Date().getMonth()+1 <10 ? '0'+ (new Date().getMonth() + 1): (new Date().getMonth()+1);
    let dd = new Date().getDate()<10 ? '0'+ new Date().getDate() : new Date().getDate();
    let years = yy.toString().slice(2)
    let gettime = years + mm +dd;
    return  gettime
}


// 获取当天日期不要时分秒
function todayTime(){
  let yy = new Date().getFullYear();
  let mm = new Date().getMonth()+1 <10 ? '0'+ (new Date().getMonth() + 1): (new Date().getMonth()+1);
  let dd = new Date().getDate()<10 ? '0'+ new Date().getDate() : new Date().getDate();
  let years = yy.toString().slice(2)
  // let hh = new Date().getHours();
  // let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
  // let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
  let gettime = years + '-' +mm + '-'+dd;
  return  gettime
}




// 获取默认时间用作给系统赋默认值时使用
function expressTime(){
    let yy = new Date().getFullYear();
    let mm = new Date().getMonth()+1 <10 ? '0'+ (new Date().getMonth() + 1): (new Date().getMonth()+1);
    let dd = new Date().getDate()<10 ? '0'+ new Date().getDate() : new Date().getDate();
    let hh = new Date().getHours();
    let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
    let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
    let gettime = yy+'-'+ mm +'-'+ dd +' '+hh+':'+ mf +':'+ ss;
    return  gettime
}



// 获取上一个月时间,返回yyyy-MM-dd字符串
function getLastMonthTime(n){
  var n = n;
  var d = new Date();
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  if (day <= n) {
    if (mon > 1) {
      mon = mon - 1;
    } else {
      year = year - 1;
      mon = 12;
    }
  }
  d.setDate(d.getDate() - n);
  year = d.getFullYear();
  mon = d.getMonth() + 1;
  day = d.getDate();
  let s =
    year +
    "-" +
    (mon < 10 ? "0" + mon : mon) +
    "-" +
    (day < 10 ? "0" + day : day);
  return s;
}

// 获取当月1号
function expressTimeMonth(){
  let yy = new Date().getFullYear();
  let mm = new Date().getMonth()+1 <10 ? '0'+ (new Date().getMonth() + 1): (new Date().getMonth()+1);
  let dd = new Date().getDate()<10 ? '0'+ new Date().getDate() : new Date().getDate();
  let hh = new Date().getHours();
  let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
  let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
  let gettime = yy+'-'+ mm +'-'+ '01' +' '+hh+':'+ mf +':'+ ss;
  return  gettime
}





function exportExcel(data, fileName){
    // 下面就是DOM操作 1.添加一个a标签 2.给a标签添加了属性 3.给他添加了点击事件(点击后移除)
    const url = window.URL.createObjectURL(new Blob([data]));
    const link = document.createElement("a");
    link.style.display = "none";
    link.href = url;
    link.setAttribute("download", fileName+'.xlsx');
    document.body.appendChild(link);
    link.click();
    link.remove();
}


function json2excel(tableJson, filenames, autowidth, bookTypes) {
  import("@/excel/Export2Excel").then(excel => {
    var tHeader = [];
    var dataArr = [];
    var sheetnames = [];
    for (var i in tableJson) {
      tHeader.push(tableJson[i].tHeader);
      dataArr.push(formatJson(tableJson[i].filterVal, tableJson[i].tableDatas));
      sheetnames.push(tableJson[i].sheetName);
    }
    excel.export_json_to_excel({
      header: tHeader,
      data: dataArr,
      sheetname: sheetnames,
      filename: filenames,
      autoWidth: autowidth,
      bookType: bookTypes
    });
  });
}



// 数据过滤，时间过滤
function formatJson(filterVal, jsonData) {
  return jsonData.map(v =>
    filterVal.map(j => {
      if (j === "timestamp") {
        return parseTime(v[j]);
      } else {
        return v[j];
      }
    })
  );
}



// 获取26个英文字母用来表示excel的列
function getCharCol(n) {
  for (var i = 0; i < this.list.length; i++) {
    this.list[i].showActive = false;
    if (index == i) {
      this.list[index].showActive = true;
    }
  }
  let temCol = "",
    s = "",
    m = 0;
  while (n > 0) {
    m = (n % 26) + 1;
    s = String.fromCharCode(m + 64) + s;
    n = (n - m) / 26;
  }
  return s;
}



/**
 * 导出
 **/
export {
    noTime,
    timestampToTime,
    setStore,
    getStore,
    removeStore,
    setCookie,
    getCookie,
    delCookie,
    expressTime,
    exportExcel,
    json2excel,
    getCharCol,
    formatJson,
    todayTime,
    getLastMonthTime,
    expressTimeMonth,
}